package org.wx;



import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.wx.entity.Person;

import java.io.IOException;

/**
 * es 客户端
 *
 * @author wang.xun
 * @date 2024/02/28
 */
@Slf4j
public class EsClient01 {

    volatile static  RestHighLevelClient esClient;

    static RestHighLevelClient initClient(String host, int port) {
        if (esClient == null) {

            synchronized (EsClient01.class) {
                if (esClient == null) {
                    esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port)));
                }
            }
        }
        return esClient;
    }


    public static void main(String[] args) throws IOException {



        log.info("开始进行 es 连接");
        RestHighLevelClient client = initClient("192.168.12.208", 9200);
        CreateIndexRequest request = new CreateIndexRequest("demo");
        CreateIndexResponse response = null;
        try {
            response = client.indices().create(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("创建索引失败",e);
            client.indices().delete(new DeleteIndexRequest("demo"), RequestOptions.DEFAULT);
            response = client.indices().create(request, RequestOptions.DEFAULT);
        }

        log.info("创建结果：{}",response.isAcknowledged());
        GetIndexResponse getIndexResponse = client.indices().get(new GetIndexRequest("student"), RequestOptions.DEFAULT);
        log.info("aliases:{}",getIndexResponse.getAliases());
        log.info("mapping:{}",getIndexResponse.getMappings());
        log.info("settings:{}",getIndexResponse.getSettings());

        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.setAge(i);
            person.setName("wanger"+i);
            person.setNickname("wanger"+i);
            person.setSex("男");
            creatDoc(esClient, "student", "1000" + i, person);
        }

        client.close();
    }

    static void creatDoc(RestHighLevelClient esClient,String indexName,String id,Object object){
        IndexRequest request = new IndexRequest();
        request.index(indexName).id(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String content;
        try {
            content = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        request.source(content, XContentType.JSON);
        try {
            IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
            log.info("_index:" + response.getIndex());
            log.info("_id:" + response.getId());
            log.info("_result:" + response.getResult());
        } catch (IOException e) {
            log.error("错误",e);
        }
    }
}
